package org.cobo.vmmanagement.web;
import java.util.Date;

import javax.persistence.TypedQuery;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cobo.vmmanagement.domain.Charge;
import org.cobo.vmmanagement.domain.Project;
import org.cobo.vmmanagement.domain.Resource;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping("/chargehours/**")
@Controller
public class ChargeHours {

    @RequestMapping(value="/chargehours/project/{projectId}", method = RequestMethod.GET)
    public String get(@PathVariable Long projectId, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    	String projectName = Project.findProject(projectId).getName();
    	modelMap.addAttribute("projectName", projectName);
    	modelMap.addAttribute("projectId", projectId);
    	return "chargehours/index";
    }
    
    @RequestMapping(method = RequestMethod.POST)
    public String post(@RequestParam("projectId") Long projectId, @RequestParam("amountOfHours") Integer amountOfHours, @RequestParam("description") String description, @RequestParam("date") Date date, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
   	
    	if(projectId == null || amountOfHours == null || description == null || date == null) {
    		 return "chargehours/index";
    	}
    	
    	// get logged in username
    	Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String userName = auth.getName(); 
        TypedQuery<Resource> resources = Resource.findResourcesByNameEquals(userName);
        Resource resource = resources.getSingleResult();
        
        // Get project
        Project project = Project.findProject(projectId);
        
        // Update total in Project
        Integer currentTotal = project.getChargedHours();
        if(currentTotal == null) {
        	currentTotal = 0;
        }
        project.setChargedHours(new Integer(currentTotal.intValue() + amountOfHours.intValue()));
        project.merge();
        
        // Save to database
        Charge charge = new Charge();
        charge.setResource(resource);
        charge.setProject(project);
        charge.setAmountOfHours(amountOfHours);
        charge.setDescription(description);
        charge.setStartingMonday(date);
        charge.persist();
        
        // Update project details and show summary
        return "forward:/projectsummary/"+project.getId().toString()+"/"+amountOfHours;
    }

    @RequestMapping
    public String index() {
        return "chargehours/index";
    }
    
    @RequestMapping(method = RequestMethod.GET)
    public String get(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    	return "chargehours/index";
    }
}
